import React, { useState } from 'react'
import axios from 'axios' //axios请求
import PubSub from 'pubsub-js' //订阅

export default function Header() {
  const [search, setSearch] = useState('')

  //获取输入框内容
  const changeSearchHandle = (e) => {
    setSearch(e.target.value)
  }


  //搜索的点击事件
  const searchHandle = async () => {
    //点击的时候发布订阅
    PubSub.publish("github" , { isInit: false, isSearch: true, data: [] })

    //发送请求
    const result = await axios.get("https://api.github.com/search/users?q=" + search);

    //请求完成发布订阅
    PubSub.publish("github", { isInit: false, isSearch: false, data: result.data.items })

  }

  return (
    <section className="jumbotron">
      <h3 className="jumbotron-heading">Search Github Users</h3>
      <div>
        <input type="text" value={search} onChange={changeSearchHandle} placeholder="enter the name you search" />
        &nbsp;
        <button onClick={searchHandle}>Search</button>
      </div>
    </section>
  )
}
